package com.efuture.springbootcondition.condition;


import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * @description: TODO
 * @author: zhaoshanshan
 * @create: 2021-10-31 19:21
 **/
public class MyConditional implements Condition {
    /**
     *
     * @param context 上下文对象 用于获取环境 IOC容器 ClassLoader对象
     * @param metadata 注解元对象 可以获取自定义注解的属性值
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//        try {
//           //需求
//            //导入了Jedis类  返回true 否则返回false
//            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
//            return true;
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//            return false;
//        }
        //return false;


        //动态获取坐标
        boolean flag = true;
        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(SelfConditionClass.class.getName());
        String[] value = (String[])annotationAttributes.get("value");
        for (String s : value) {
            try {
                Class<?> cls = Class.forName(s);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;

    }
}
